﻿package com.bizmlib.controls
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	
	public class CropToolCursor extends MovieClip
	{
		public static const EVENT_DRAG_INVOKED:String = "dragInvoked";
		public static const EVENT_DRAG_STOP_INVOKED:String = "dragStopInvoked";
		public static const EVENT_DRAGGING:String = "dragging";
		
		private var __top:Boolean = true;
		private var __left:Boolean = true;
		private var __middle:Boolean = false;
		private var __width:Number;
		private var __height:Number;
		private var __dragBounds:Rectangle;
		
		
		public function CropToolCursor()
		{
			init();
		}
		
		
		public function set top(val:Boolean):void
		{
			__top = val;
		}
		public function set left(val:Boolean):void
		{
			__left = val;
		}
		public function set middle(val:Boolean):void
		{
			__middle = val;
		}
		public function get middle():Boolean
		{
			return __middle;
		}
		public function get isLeft():Boolean
		{
			return middle ? false : __left;
		}
		public function get isRight():Boolean
		{
			return middle ? false : (!__left);
		}
		public function get isTop():Boolean
		{
			return middle ? false : __top;
		}
		public function get isBottom():Boolean
		{
			return middle ? false : (!__top);
		}
		/**
		public function set width(value:Number):Void
		{
			__width = value;
			updateWidth();
		}
		public function get width():Number
		{
			return __width;
		}
		public function set height(value:Number):Void
		{
			__height = value;
			updateHeight();
		}
		public function get height():Number
		{
			return __height;
		}
		/**/
		public function get dragBounds():Rectangle
		{
			return __dragBounds;
		}
		
		
		override public function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void
		{
			__dragBounds = bounds;
		}
		override public function stopDrag():void
		{
		}
		
		
		private function init():void
		{
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
			
			__dragBounds = new Rectangle(0, 0, 0, 0);
		}
		
		private function addedToStage(event:Event):void
		{
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
		}
		private function removedFromStage(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
			
			removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMovePhase1);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMovePhase2);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			
			__dragBounds = null;
		}
		
		private function mouseDown(event:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMovePhase1);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		private function mouseMovePhase1(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMovePhase1);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMovePhase2);
			
			dispatchEvent(new Event(EVENT_DRAG_INVOKED, false, true));
		}
		private function mouseMovePhase2(event:MouseEvent):void
		{
			dispatchEvent(new Event(EVENT_DRAGGING, false, true));
		}
		private function mouseUp(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMovePhase2);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			
			dispatchEvent(new Event(EVENT_DRAG_STOP_INVOKED, false, true));
		}
		/**
		private function updateWidth():void
		{
		}
		private function updateHeight():void
		{
		}
		/**/
	}
}